package oaSystem.biz.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import oaSystem.Exception.BizException;
import oaSystem.biz.EventDetailsBiz;
import oaSystem.entity.Activity;
import oaSystem.entity.ActivityActor;
import oaSystem.entity.Employee;
import oaSystem.entity.Event;
import oaSystem.entity.EventDetails;
import oaSystem.mapper.ActivityActorMapper;
import oaSystem.mapper.ActivityMapper;
@Service
public class EventDetailsBizImpl implements EventDetailsBiz {
	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private ActivityActorMapper activityActorMapper;
	
	/**
	 * 增加活动&活动参与者
	 * @param eventDetails
	 * @param creator
	 * @throws ParseException
	 */
	@Transactional(propagation=Propagation.REQUIRED, timeout=60)
	@Override
	public void addEventDetails(EventDetails eventDetails, Employee creator) throws ParseException {
		//增加activity
		Event event = eventDetails.getEvent();
		if(event==null){
			return;
		}
		Activity activity = formatActivity(event,creator);
		activityMapper.addActivity(activity);
		
		//循环增加activityActor
		List<Employee> actorList = eventDetails.getActorList();
		if(actorList!=null&&actorList.size()>0){
			ActivityActor activityActor = new ActivityActor();
			activityActor.setActivity(activity);
			//循环添加参与者
			for (Employee employee : actorList) {
				activityActor.setActor(employee);
				activityActorMapper.addActivityActor(activityActor);
			}
		}

	}
	
	/**
	 * 编辑活动&活动参与者
	 * @param eventDetails
	 * @param creator
	 * @throws ParseException
	 * @throws BizException 
	 */
	@Transactional(propagation=Propagation.REQUIRED, timeout=60)
	@Override
	public void updateEventDetails(EventDetails eventDetails, Employee creator,Boolean isCompleteChange) throws ParseException, BizException {
		//编辑activity
		Event event = eventDetails.getEvent();
		if(event==null){
			throw new BizException("更新活动异常，无法获取活动");
		}
		String idStr = event.getId();
		
		if(idStr==null||idStr.isEmpty()){
			throw new BizException("更新活动异常，无法获取活动Id");
		}
		Activity activity = activityMapper.fetchActivityById(Integer.parseInt(idStr));
		
		//检测修改人和创建人是否是同一人
		checkUpdateUser(creator,activity);
		
		if(isCompleteChange){
			activity = formatActivity(event, creator);
			activity.setId(Integer.parseInt(idStr));
		}else{
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date start = format.parse(event.getStart());
			Date end = format.parse(event.getEnd());
			activity.setStartTime(start);
			activity.setEndTime(end);
		}
		
		activityMapper.updateActivity(activity);
		
		//循环增加activityActor
		List<Employee> actorList = eventDetails.getActorList();
		if(actorList!=null&&actorList.size()>0){
			ActivityActor activityActor = new ActivityActor();
			activityActor.setActivity(activity);
			//循环添加参与者
			for (Employee employee : actorList) {
				activityActor.setActor(employee);
				activityActorMapper.addActivityActor(activityActor);
			}
		}

	}
	
	/**
	 * 查询活动&参与者（by activityId）
	 * @param activityId
	 */
	@Override
	public EventDetails fetchEventDetailsByActivityId(Integer activityId) {
		EventDetails eventDetailsList=new EventDetails();
		Activity activity = activityMapper.fetchActivityById(activityId);
		List<ActivityActor> activityList = activityActorMapper.findActivityActorByActivityId(activityId);
		
		//保存活动
		Event event =parseEvent(activity);
		eventDetailsList.setEvent(event);
		
		//保存参与者
		List<Employee> list=null;
		if(activityList!=null&&activityList.size()>0){
			list = new ArrayList<Employee>();
			Employee employee = null;
			for (ActivityActor activityActor : activityList) {
				employee= new Employee();
				employee=activityActor.getActor();
				list.add(employee);
			}
		}
		eventDetailsList.setActorList(list);
		
		return eventDetailsList;
	}
	
	/**
	 * 抽取方法（格式化Activity）
	 * @param event
	 * @param creator
	 * @return
	 * @throws ParseException
	 */
	private Activity formatActivity(Event event,Employee creator) throws ParseException{
		Activity activity = new Activity();
		activity.setColor(event.getColor());
		activity.setCreateTime(new Date());
		activity.setCreator(creator);
		activity.setDescription(event.getDescription());
		//判断是否全日
		String allDayStr = event.getAllDay();
		Boolean isAllDay=false;
		if(allDayStr!=null&&!allDayStr.isEmpty()){
			isAllDay=Boolean.parseBoolean(allDayStr);
		}
		SimpleDateFormat format = checkIsAllDay(isAllDay);
		Date startDate=null;
		Date endDate=null;
		
		String end = event.getEnd();
		if(end!=null&&!end.isEmpty()){
			endDate=format.parse(end);
		}
		String start = event.getStart();
		if(start!=null&&!start.isEmpty()){
			startDate=format.parse(start);
		}
		activity.setEndTime(endDate);
		activity.setIsAllDay(isAllDay);
		activity.setLocation(event.getLocation());
		activity.setStartTime(startDate);
		activity.setTitle(event.getTitle());
		return activity;
	}
	
	/**
	 * 抽取方法（判断是否全日）
	 * @param isAllDay
	 * @return
	 */
	private SimpleDateFormat checkIsAllDay(Boolean isAllDay) {
		SimpleDateFormat format=null;
		if(isAllDay){
			//全天
			format=new SimpleDateFormat("yyyy-MM-dd");
		}else{
			//不是全天
			format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		}
		return format;
	}
	
	
	/**
	 * 抽取方法（Activity转Event）
	 * @param activity
	 * @return
	 */
	private Event parseEvent(Activity activity) {
		Event event=null;
		if(activity!=null){
			event=new Event();
			Boolean isAllDay = activity.getIsAllDay();
			event.setAllDay(isAllDay.toString());
			event.setColor(activity.getColor());
			event.setDescription(activity.getDescription());
			event.setId(activity.getId().toString());
			event.setLocation(activity.getLocation());
			event.setTitle(activity.getTitle());
			SimpleDateFormat format = checkIsAllDay(isAllDay);
			event.setStart(format.format(activity.getStartTime()));
			Date endTime = activity.getEndTime();
			if(endTime==null){
				event.setEnd("");
			}else{
				event.setEnd(format.format(endTime));
			}
		}
		return event;
	}
	/**
	 * 抽取方法（检测修改人和创建人是否是同一人）
	 * @param creator
	 * @param activity
	 * @throws BizException 
	 */
	private void checkUpdateUser(Employee editor, Activity activity) throws BizException {
		if(activity.getCreator().getId()!=editor.getId()){
			throw new BizException("你不是活动创建者，无法修改此活动");
		}
	}
}
